package org.cqut.system.course.controller;

import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.course.domain.Course;
import org.cqut.system.course.service.CourseService;
import org.cqut.system.user.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

/**
 * @author XiAoXu
 * @date 2021/11/2 21:48
 * @description
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @RequestMapping(method = RequestMethod.GET, path = "/list")
    public ResultInfo queryCourse(@RequestParam String condition) {
        List<Course> course = courseService.getCourseList(condition);
        if (course != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, course);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @RequestMapping(method = RequestMethod.GET, path = "/listForStudent/{userId}")
    public ResultInfo queryCourseByStudentId(@PathVariable("userId") String userId) {
        List<Course> course = courseService.queryCourseByStudentId(userId);
        if (course != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, course);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResultInfo saveCourse(@RequestBody Course course) {
        boolean flag = courseService.save(course);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @RequestMapping(method = RequestMethod.GET, path = "/details/{courseId}")
    public ResultInfo queryCourseById(@PathVariable("courseId") Long courseId) {
        Course course = courseService.getById(courseId);
        if (course != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, course);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @RequestMapping(method = RequestMethod.DELETE, path = "/{courseId}")
    public ResultInfo deleteCourseById(@PathVariable String courseId) {
        boolean flag = courseService.removeById(courseId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResultInfo updateCourseById(@RequestBody Course course) {
        boolean flag = courseService.updateById(course);
        if (flag) {
            return new ResultInfo(Status.UPDATE_DETAILS_SUCCESS.code, Status.UPDATE_DETAILS_SUCCESS.message);
        } else {
            return new ResultInfo(Status.UPDATE_DETAILS_ERROR.code, Status.UPDATE_DETAILS_ERROR.message);
        }
    }

    @RequestMapping(method = RequestMethod.GET,path = "/studentsForCourse/{courseId}")
    public ResultInfo queryStudentForCourse(@PathVariable String courseId){
        List<UserVo> studentList = courseService.getStudentForCourse(courseId);
        if (studentList != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, studentList);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }
}
